package com.mdd.front.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.front.LikeFrontThreadLocal;
import com.mdd.front.service.IAiQuestionRecordService;
import com.mdd.front.validate.AiQuestionRecordCreateValidate;
import com.mdd.front.validate.AiQuestionRecordUpdateValidate;
import com.mdd.front.validate.AiQuestionRecordSearchValidate;
import com.mdd.front.validate.common.PageValidate;
import com.mdd.front.vo.AiQuestionRecordListedVo;
import com.mdd.front.vo.AiQuestionRecordDetailVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.AiQuestionRecord;
import com.mdd.common.mapper.AiQuestionRecordMapper;
import com.mdd.common.util.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * ai聊天记录实现类
 * @author XueYong
 */
@Service
public class AiQuestionRecordServiceImpl implements IAiQuestionRecordService {
        
    @Resource
    AiQuestionRecordMapper aiQuestionRecordMapper;

    /**
     * ai聊天记录列表
     *
     * @author XueYong
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<AiQuestionRecordListedVo>
     */
    @Override
    public PageResult<AiQuestionRecordListedVo> list(PageValidate pageValidate, AiQuestionRecordSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        Integer userId = LikeFrontThreadLocal.getUserId();
        QueryWrapper<AiQuestionRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("session_id",searchValidate.getSessionId())
                .eq("user_id",userId)
                .orderByDesc("id");

        aiQuestionRecordMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:userId@user_id:long",
            "=:type:long",
            "like:content:str",
        });

        IPage<AiQuestionRecord> iPage = aiQuestionRecordMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<AiQuestionRecordListedVo> list = new LinkedList<>();
        for(AiQuestionRecord item : iPage.getRecords()) {
            AiQuestionRecordListedVo vo = new AiQuestionRecordListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * ai聊天记录详情
     *
     * @author XueYong
     * @param id 主键参数
     * @return AiQuestionRecord
     */
    @Override
    public AiQuestionRecordDetailVo detail(Integer id) {
        AiQuestionRecord model = aiQuestionRecordMapper.selectOne(
                new QueryWrapper<AiQuestionRecord>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        AiQuestionRecordDetailVo vo = new AiQuestionRecordDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * ai聊天记录新增
     *
     * @author XueYong
     * @param createValidate 参数
     */
    @Override
    public void add(AiQuestionRecordCreateValidate createValidate) {
        AiQuestionRecord model = new AiQuestionRecord();
        model.setSessionId(createValidate.getSessionId());
        model.setUserId(createValidate.getUserId());
        model.setType(createValidate.getType());
        model.setContent(createValidate.getContent());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        aiQuestionRecordMapper.insert(model);
    }

    /**
     * ai聊天记录编辑
     *
     * @author XueYong
     * @param updateValidate 参数
     */
    @Override
    public void edit(AiQuestionRecordUpdateValidate updateValidate) {
        AiQuestionRecord model = aiQuestionRecordMapper.selectOne(
                new QueryWrapper<AiQuestionRecord>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setUserId(updateValidate.getUserId());
        model.setType(updateValidate.getType());
        model.setContent(updateValidate.getContent());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        aiQuestionRecordMapper.updateById(model);
    }

    /**
     * ai聊天记录删除
     *
     * @author XueYong
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        AiQuestionRecord model = aiQuestionRecordMapper.selectOne(
                new QueryWrapper<AiQuestionRecord>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        aiQuestionRecordMapper.delete(new QueryWrapper<AiQuestionRecord>().eq("id", id));
    }

}
